package com.ansion.basedb.dao.helper;

import android.Manifest;
import android.content.ContentResolver;
import android.net.Uri;
import android.provider.ContactsContract;

import com.ansion.basedb.dao.bean.SyncAdapterColumns;
import com.ansion.log.VLog;
import com.ansion.utils.ContactUtil;
import com.ansion.utils.ResourceUtils;
import com.ansion.utils.permission.RealRxPermission;


public class SyncContactHelper {

    public static void deleteContactFromPhoneBook(String uid) {
        if (!RealRxPermission.getInstance().isGranted(Manifest.permission.READ_CONTACTS)) {
            return;
        }
        try {
            VLog.i("SyncContactsThread", "deleteContactFromPhoneBook start for %s", uid);
            ContentResolver contentResolver = ResourceUtils.getApplication().getContentResolver();
            Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, ContactUtil.getMyUid()).appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, SyncAdapterColumns.ACCOUNT_TYPE).build();
            int value = contentResolver.delete(rawContactUri, ContactsContract.RawContacts.SYNC2 + " = " + uid, null);
            VLog.i("SyncContactsThread", "deleteContactFromPhoneBook success for %s", uid);
        } catch (Exception e) {
            VLog.e(e, "SyncContactsThread", "deleteContactFromPhoneBook for:%s", uid);
        }
    }

}
